package com.base.card.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.card.domain.TCryptCardCertHistory;
import com.base.card.mapper.TCryptCardCertHistoryMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.card.domain.bo.TCryptCardCertHistoryBo;
import com.base.card.domain.vo.TCryptCardCertHistoryVo;
import com.base.card.service.ITCryptCardCertHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 密码卡历史证书Service业务层处理
 *
 * @author base
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class TCryptCardCertHistoryServiceImpl implements ITCryptCardCertHistoryService {

    private final TCryptCardCertHistoryMapper baseMapper;

    /**
     * 查询密码卡历史证书
     */
    @Override
    public TCryptCardCertHistoryVo queryById(String autoId){
        return baseMapper.selectVoById(autoId);
    }

    /**
     * 查询密码卡历史证书列表
     */
    @Override
    public TableDataInfo<TCryptCardCertHistoryVo> queryPageList(TCryptCardCertHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TCryptCardCertHistory> lqw = buildQueryWrapper(bo);
        Page<TCryptCardCertHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询密码卡历史证书列表
     */
    @Override
    public List<TCryptCardCertHistoryVo> queryList(TCryptCardCertHistoryBo bo) {
        LambdaQueryWrapper<TCryptCardCertHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TCryptCardCertHistory> buildQueryWrapper(TCryptCardCertHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TCryptCardCertHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCardCode()), TCryptCardCertHistory::getCardCode, bo.getCardCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPubkey()), TCryptCardCertHistory::getPubkey, bo.getPubkey());
        lqw.eq(StringUtils.isNotBlank(bo.getCertSn()), TCryptCardCertHistory::getCertSn, bo.getCertSn());
        lqw.eq(StringUtils.isNotBlank(bo.getCertDn()), TCryptCardCertHistory::getCertDn, bo.getCertDn());
        lqw.eq(bo.getStartTime() != null, TCryptCardCertHistory::getStartTime, bo.getStartTime());
        lqw.eq(bo.getExpireTime() != null, TCryptCardCertHistory::getExpireTime, bo.getExpireTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TCryptCardCertHistory::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getCertStatus()), TCryptCardCertHistory::getCertStatus, bo.getCertStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCaCertContent()), TCryptCardCertHistory::getCaCertContent, bo.getCaCertContent());
        lqw.eq(StringUtils.isNotBlank(bo.getEncPrikeyContent()), TCryptCardCertHistory::getEncPrikeyContent, bo.getEncPrikeyContent());
        lqw.eq(StringUtils.isNotBlank(bo.getEncCertContent()), TCryptCardCertHistory::getEncCertContent, bo.getEncCertContent());
        return lqw;
    }

    /**
     * 新增密码卡历史证书
     */
    @Override
    public Boolean insertByBo(TCryptCardCertHistoryBo bo) {
        TCryptCardCertHistory add = BeanUtil.toBean(bo, TCryptCardCertHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAutoId(add.getAutoId());
        }
        return flag;
    }

    /**
     * 修改密码卡历史证书
     */
    @Override
    public Boolean updateByBo(TCryptCardCertHistoryBo bo) {
        TCryptCardCertHistory update = BeanUtil.toBean(bo, TCryptCardCertHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TCryptCardCertHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除密码卡历史证书
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
